#pragma once 

#include <iostream>
#include <string>
#include <ctemplate/template.h>

#include "../comm/log.hpp"
#include "../comm/util.hpp"
#include "./oj_model2.hpp"


namespace ns_view
{
    const std::string template_path = "./template_html/";
    using namespace ns_util;
    using namespace ns_logUtil;
    using namespace ns_model;

    class View
    {
    private:

    public:
        View()
        {}
        ~View()
        {}

    public:
        bool AllExpandHtml(const std::vector<struct Question> &questions, std::string *html)
        {
            std::string src = template_path + "all_question.html";

            ctemplate::TemplateDictionary root("all_question");

            // 形成数据字典
            for(const auto &q : questions)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                // 题目编号 题目名称 题目难度
                sub->SetValue("number", q.number);
                sub->SetValue("title", q.title);
                sub->SetValue("star", q.star);
            }

            // 获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src, ctemplate::DO_NOT_STRIP); // DO_NOT_STRIP:不需要清除空格空行


            // 开始完成渲染
            tpl->Expand(html, &root);

            return true;
        }

        bool OneExpandHtml(const struct Question &q, std::string *html)
        {   
            // 1.形成路径
            std::string src = template_path + "one_question.html";

            // 2.形成数据字典
            ctemplate::TemplateDictionary root("one_question");
            
            root.SetValue("number", q.number);
            root.SetValue("title", q.title);
            root.SetValue("star", q.star);
            root.SetValue("desc", q.desc);
            root.SetValue("pre_code", q.header);

            // 3.获取被渲染的html
            ctemplate::Template *tpl = ctemplate::Template::GetTemplate(src, ctemplate::DO_NOT_STRIP);

            // 4.开始完成渲染
            tpl->Expand(html, &root);
            return true;
        }

    };
}